﻿using System.Collections;
using UnityEngine;

/// <summary>
/// 游戏流程
/// </summary>
public class GameProcedure : BaseProceduar
{

	///////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// 游戏UI
	/// </summary>
	UIGameWindow _ui;

	/// <summary>
	/// 游戏状态
	/// </summary>
	GameState _curState;

	///////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// 单例
	/// </summary>
	static GameProcedure _inst;

	public static GameProcedure inst {
		get {
			if (_inst == null) {
				_inst = new GameProcedure ();
			}
			return _inst;
		}
	}

	/// <summary>
	/// 切换游戏状态
	/// </summary>
	/// <param name="state"></param>
	public void SwitchState (GameState state)
	{
		if (_curState != null) {
			_curState.Leave ();
		}

		_curState = state;

		if (_curState != null) {
			_curState.Enter ();
		}
	}

	///////////////////////////////////////////////////////////////////////////

	public override void EnterProceduar (bool initUI)
	{
		DebugInfo.Log ("进入游戏流程");
		SwitchState (Game_JoinRoom1State.inst);
	}

	public override void UpdateProceduar ()
	{
		base.UpdateProceduar ();
	}

	public override void LeaveProceduar ()
	{
		DebugInfo.Log ("退出游戏流程");

		UIGameLeaveConfirm.CloseWindow ();
	}

	protected override void HandleMessage (Message message)
	{
		if (_curState != null && _curState.HandleMessage (message)) {
			return;
		}

		switch (message.msgId) {
		case Commands.CMD_CTRL_BUTTON_CLICKED:
			switch (message.iParam) {
			case GameCommands.CMD_INVITE_FRIENDS:
				// 邀请好友
				#if UNITY_ANDROID
				string title = "悠闲干瞪眼 房间号[" + GameDataKernel.inst.roomId + "]";
				string desc = "房间号：" + GameDataKernel.inst.roomId
				              + "，轮数：" + GameDataKernel.inst.round
				              + "，癞子数：" + GameDataKernel.inst.laiziNum + "，速度来玩！";
				WeChat.inst.InviteFriend (false, title, Constants.SHARE_URL, desc);
				#endif
				break;
			case GameCommands.CMD_SHARE_GAME_SETT:
				// 分享战绩
				#if UNITY_ANDROID
				WeChat.inst.ShareImage(GameObject.Find("Stage Camera").GetComponent<Camera>());
				#endif
				break;
			default:
				break;
			}
			break;

		default:
			base.HandleMessage (message);
			break;
		}
	}

	protected override bool ProcessPacket (IOMessage message)
	{
		if ((_curState != null) && _curState.ProcessPacket (message)) {
			return true;
		}

		switch (message.code) {
		case IOCmds.SMSG_BROADCAST_MESSAGE:
			GameDataKernel.inst.HandleMessage (message);
			return true;
		case IOCmds.SMSG_SET_PLAYER_EXIT_ROOM:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_PLAYER_REQ_DROOM:
			UIGameIpTip.CloseWindow ();
			UIGameLeaveConfirm.Popup ();
			UIGameLeaveConfirm.HandleMessage (message);
			return true;
		case IOCmds.SMSG_SET_PLAYER_RESP_DROOM:
			UIGameLeaveConfirm.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_PLAYER_OFFLINE:
			GameDataKernel.inst.HandleMessage (message);
			return true;
		case IOCmds.SMSG_SET_PLAYER_ONLINE:
			GameDataKernel.inst.HandleMessage (message);
			return true;
		}

		return base.ProcessPacket (message);
	}

	public void InitUI ()
	{
		UIHome_CreateRoom.CloseWindow ();
		UIHome_JoinRoom.CloseWindow ();

		_ui = new UIGameWindow ();
		UIRootController.inst.SwitchTo (_ui);

		DebugInfo.Log ("游戏界面UI初始化完毕");

		AudioController.inst.PlayBGM ("youxizhong");
	}
}
